$(function () {
    $("#tou").load("tou.html");
    showAddress();

    $("#addAddress").click(function () {
        $("#sss").show();
        $("#q").text("添加地址");
        $("#updateAddress").text("添加");
        $("#updateAddress").attr("id","add");
        $("[name='address']").val("");
        $("[name='remark']").val("");
        //添加用户地址
        $("#add").unbind("click").click(function () {
            var address ="";
            var selsect1=$("#select1").val();
            var selsect2=$("#select2").val();
            var selsect3=$("#select3").val();
            address += selsect1;
            address += selsect2;
            address += selsect3;
            if(selsect1 =="" || selsect1 == null || selsect1 == undefined){
                alert("请填写省级详细地址");
                return;
            }
            if(selsect2 =="" || selsect2 == null || selsect2 == undefined){
                alert("请填写市级详细地址");
                return;
            }
            if(selsect3 =="" || selsect3 == null || selsect3 == undefined){
                alert("请填写区级详细地址");
                return;
            }
            var address2 = $("[name='address']").val();
            address +=address2;
            if(address2 == ""){
                alert("请填写详细地址")
                return;
            }
            var remark = $("[name='remark']").val();
            $.ajax({
                url: "easybuy/addUserAddress",
                data: {
                    address: address,
                    remark: remark
                },
                success: function (data) {
                    if (data == "success") {
                        alert("添加成功！");
                        $("[name='address']").val("");
                        $("[name='remark']").val("");
                        showAddress();
                    } else {
                        alert("添加失败，请联系管理员！");
                    }
                }
            })
        })
    });

});

//加载显示用户地址
function showAddress() {
    $.ajax({
        url: "easybuy/userAddress",
        success: function (data) {
            $("#orderForm").empty();

            $("#orderForm").append("<tr>\n" +
                "                    <td width=\"10%\">默认地址</td>\n" +
                "                    <td width=\"30%\">地址</td>\n" +
                "                    <td width=\"15%\">创建时间</td>\n" +
                "                    <td width=\"25%\">备注</td>\n" +
                "                    <td width=\"20%\">操作</td>\n" +
                "                </tr>");

            $.each(data, function (index, user) {
                if (user.isdefault == 0) {
                    $("#orderForm").append("<tr>\n" +
                        "                    <td><input type=\"radio\" name='default' data-id='"+user.id+"'></td>\n" +
                        "                    <td><font color=\"#ff4e00\">" + user.address + "</font></td>\n" +
                        "                    <td>" + user.createtime + "</td>\n" +
                        "                    <td>" + user.remark + "</td>\n" +
                        "                    <td><a href='javascript:;' data-id='" + user.id + "' class='delete'>删除</a>&nbsp;&nbsp;&nbsp;" +
                        "                        <a href='javascript:;' data-id='" + user.id + "' class='update'>修改</a>" +
                        "                    </td>\n" +
                        "                     </tr>");
                } else {
                    $("#orderForm").append("<tr>\n" +
                        "                    <td><input type=\"radio\" name='default' checked data-id='"+user.id+"'></td>\n" +
                        "                    <td><font color=\"#ff4e00\">" + user.address + "</font></td>\n" +
                        "                    <td>" + user.createtime + "</td>\n" +
                        "                    <td>" + user.remark + "</td>\n" +
                        "                    <td>" +
                        "                           <a href='javascript:;' data-id='" + user.id + "' class='update'>修改</a></td>\n" +
                        "                       </tr>");
                }

            });
            deleteAddress();

            updateAddress();

            $("[name='default']").change(function () {
                updateDefault($(this).data("id"));
            })
        }
    });
}

//删除地址
function deleteAddress() {
    $(".delete").click(function () {
        if (confirm("是否要删除该地址")) {
            var id = $(this).data("id");
            $.ajax({
                url: "easybuy/deleteAddress",
                data: {
                    id:id
                },
                success:function (data) {
                    if (data=="success"){
                        alert("删除成功！");
                        showAddress();
                    }else {
                        alert("删除失败，请联系管理员！");
                    }
                }
            })
        }
    })
}

//修改地址
function updateAddress() {

    //地址回显
    $(".update").unbind("click").click(function () {
        $("#sss").show();
        $("#q").text("修改地址");
        var id=$(this).data("id");
        $("[name='id']").val(id);
        $("#add").text("修改");
        $("#add").attr("id","updateAddress");
        //修改
        $("#updateAddress").unbind("click").click(function () {
            var address ="";
            var selsect1=$("#select1").val();
            var selsect2=$("#select2").val();
            var selsect3=$("#select3").val();
            address += selsect1;
            address += selsect2;
            address += selsect3;
            if(selsect1 =="" || selsect1 == null || selsect1 == undefined){
                alert("请填写省级详细地址");
                return;
            }
            if(selsect2 =="" || selsect2 == null || selsect2 == undefined){
                alert("请填写市级详细地址");
                return;
            }
            if(selsect3 =="" || selsect3 == null || selsect3 == undefined){
                alert("请填写区级详细地址");
                return;
            }
            var address2 = $("[name='address']").val();
            address +=address2;
            if(address2 == ""){
                alert("请填写详细地址")
                return;
            }
            var remark = $("[name='remark']").val();
            var id=$("[name='id']").val();
            $.ajax({
                url: "easybuy/updateAddress",
                data:{
                    id:id,
                    address:address,
                    remark:remark,
                },
                success:function (data) {
                    if (data=="success"){
                        alert("修改成功！");
                        showAddress();
                    }else {
                        alert("修改失败，请联系管理员！");
                    }
                }
            });
        })
    });

}

//修改默认地址
function updateDefault(id) {
    $.ajax({
        url:"easybuy/updateIsDefault",
        data:{
            id:id
        },
        success:function (data) {
            if (data=="success"){
                alert("修改成功！");
                showAddress();
            }else {
                alert("修改失败，请联系管理员！");
            }
        }
    })
}

